using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;
using System.Xml;

namespace Acms.Bll
{
    public class PostControl : BaseItem
    {
        protected PostControl() : base() { }
        protected PostControl(Hashtable table) : base(table) { }
        protected PostControl(Hashtable table, List<PostControl> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new PostControlDal();
        }

        public static PostControl CreateInstance()
        {
            return new PostControl();
        }

        internal static PostControl CreateInstance(int postId, int templateContolId, XmlDocument value)
        {
            PostControl tmp = new PostControl();
            tmp.PostId = postId;
            tmp.TemplateControlId = templateContolId;
            tmp.Value = value;
            return tmp;
        }


        private static PostControl Get(Hashtable table)
        {
            if (table != null)
            {
                return new PostControl(table);
            }
            else
            {
                return null;
            }
        }

        private static List<PostControl> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<PostControl> tmp = new List<PostControl>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new PostControl(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static PostControl GetById(int id)
        {
            PostControlDal dal = new PostControlDal();
            return Get(dal.GetById(id));
        }


        public static List<PostControl> GetAllByPostId(int postId)
        {
            PostControlDal dal = new PostControlDal();
            return Get(dal.GetAllByPostId(postId));
        }

        public static PostControl GetByPostIdAndTemplateControlId(int postId, int templateControlId)
        {
            PostControlDal dal = new PostControlDal();
            return Get(dal.GetByPostIdAndTemplateControlId(postId, templateControlId));
        }

        public static List<PostControl> GetAllBySiteIdAndTypeName(int siteId, string typeName, DateTime? siteVersion)
        {
            PostControlDal dal = new PostControlDal();
            return Get(dal.GetAllBySiteIdAndTypeName(siteId, typeName, siteVersion));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override object SaveChanges(ref object concurencyId, Base dal)
        {
            int refId = Id;
            ((PostControlDal)dal).Update(ref refId, PostId, TemplateControlId, Value, ref concurencyId);
            Id = refId;
            return Id;
        }
        
        public new void Delete()
        {
            if (Id > 0)
            {
                base.Delete();
            }
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((PostControlDal)dal).Delete(Id, concurencyId);
        }

        protected override void Load(Hashtable table)
        {
            Id = (int)table[PostControlDal.Fields.Id];
            PostId = (int)table[PostControlDal.Fields.PostId];
            TemplateControlId = (int)table[PostControlDal.Fields.TemplateControlId];
            Name = (string)table[PostControlDal.Fields.Name];
            TypeId = (int)table[PostControlDal.Fields.TypeId];
            TypeName = (string)table[PostControlDal.Fields.TypeName];
            AdminControlType = (string)table[PostControlDal.Fields.AdminControlType];
            AdminDisplayControlType = (string)table[PostControlDal.Fields.AdminDisplayControlType];
            AdminEditControlType = (string)table[PostControlDal.Fields.AdminEditControlType];
            FrontEndControlType = (string)table[PostControlDal.Fields.FrontEndControlType];
            Value = (System.Xml.XmlDocument)table[PostControlDal.Fields.Value];
            Notes = (string)table[PostControlDal.Fields.Notes];
            ControlNotes = (string)table[PostControlDal.Fields.ControlNotes];
            Sequence = (int)table[PostControlDal.Fields.Sequence];
        }

        public int Id { get; private set; }

        public int PostId { get; private set; }

        public int TemplateControlId { get; private set; }

        public string Name { get; protected set; }

        public int TypeId { get; private set; }

        public string TypeName { get; private set; }

        public string AdminControlType { get; private set; }

        public string AdminDisplayControlType { get; private set; }
        
        public string AdminEditControlType { get; private set; }
        
        public string FrontEndControlType { get; private set; }

        public XmlDocument Value { get; set; }

        public string Notes { get; private set; }
        
        public string ControlNotes { get; private set; }

        public int Sequence { get; private set; }

        public T GetProperties<T>()
        {
            if (this.Value != null)
            {
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(T));
                return (T)xmls.Deserialize(new System.IO.StringReader(this.Value.OuterXml));
            }
            else
            {
                return default(T);
            }
        }


    }
}
